package com.lizonghui.googleplay.control;

import android.view.View;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;

import com.lizonghui.googleplay.R;
import com.lizonghui.googleplay.base.BaseHolder;
import com.lizonghui.googleplay.bean.DownLoadInfo;
import com.lizonghui.googleplay.bean.ItemInfoBean;
import com.lizonghui.googleplay.config.Constants;
import com.lizonghui.googleplay.manager.DownLoadManger;
import com.lizonghui.googleplay.utils.CommonUtils;
import com.lizonghui.googleplay.utils.LogUtils;
import com.lizonghui.googleplay.utils.StringUtils;
import com.lizonghui.googleplay.utils.UIUtils;
import com.lizonghui.googleplay.views.ProgressView;

import java.io.File;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import it.sephiroth.android.library.picasso.Picasso;

/**
 * Created by Administrator on 2016/6/19.
 * 抽取holder干的事情
 * 1提供视图
 * 2接收数据
 * 3数据和视图的绑定
 */
public class ItemHolder extends BaseHolder<ItemInfoBean> implements DownLoadManger.DownLoadInfoObserver {


    @Bind(R.id.item_appinfo_iv_icon)
    ImageView mItemAppinfoIvIcon;
    @Bind(R.id.item_appinfo_tv_title)
    TextView mItemAppinfoTvTitle;
    @Bind(R.id.item_appinfo_rb_stars)
    RatingBar mItemAppinfoRbStars;
    @Bind(R.id.item_appinfo_tv_size)
    TextView mItemAppinfoTvSize;
    @Bind(R.id.item_appinfo_tv_des)
    TextView mItemAppinfoTvDes;
    @Bind(R.id.item_appinfo_progressView)
    ProgressView mProgressView;
    public ItemInfoBean mInfoBean;


    /**
     * @param data
     * @des 绑定数据
     * @call 外界需要传递数据，让那个我进行数据绑定
     */
    @Override
    public void refreshHolderView(ItemInfoBean data) {
        //重置progressView，因为这个视图只有在下载的时候才回去刷新
        mProgressView.setProgress(0);

        mInfoBean = data;
        mItemAppinfoTvDes.setText(data.des);
        mItemAppinfoTvSize.setText(StringUtils.formatFileSize(data.size));
        mItemAppinfoTvTitle.setText(data.name);

        //图片处理：使用picasso.还可以设置裁剪，设置错误结果，设置旋转等等
        Picasso.with(UIUtils.getContext())//上下文
                .load(Constants.URLS.IMABASEURL + data.iconUrl)//图片url地址
                .fade(200)//淡入淡出效果.
                .into(mItemAppinfoIvIcon);//展示到哪里

        /**
         * ratingbar
         *  -->展示功能-->不给你评分。3.5就3.5颗星
         *  -->评分功能 -->android:isIndicator="true"可以评分
         */
        mItemAppinfoRbStars.setRating((float) data.stars);



        /*-------------2-根据不同状态，显示不同UI----------------------*/
        //首先，通过ItemInfoBean来得到downLoadInfo
        DownLoadInfo downLoadInfo = DownLoadManger.getInstance().getDownLoadInfo(data);
        refreshProgressView(downLoadInfo);

    }

    //根据不同状态刷新UI-->icon和note
    private void refreshProgressView(DownLoadInfo downLoadInfo) {
        int currentState = downLoadInfo.currentState;
        switch (currentState) {
            case DownLoadManger.STATE_UNDOWNLOAD:
                mProgressView.setTv_note("下载");
                mProgressView.setIv_icon(R.drawable.ic_download);
                break;
            case DownLoadManger.STATE_DOWNLOADING:
                mProgressView.setIv_icon(R.drawable.ic_pause);
                int progress = (int) (downLoadInfo.progress * 1.0f / downLoadInfo.size * 100 + .5f);
                mProgressView.setTv_note("下载中.." + progress + "%");
                mProgressView.setMax(downLoadInfo.size);
                mProgressView.setProgress(downLoadInfo.progress);
                break;
            case DownLoadManger.STATE_PAUSEDOWNLOAD:
                mProgressView.setTv_note("暂停");
                mProgressView.setIv_icon(R.drawable.ic_resume);
                break;
            case DownLoadManger.STATE_WAITINGDOWNLOAD:
                mProgressView.setTv_note("等待");
                mProgressView.setIv_icon(R.drawable.ic_pause);
                break;
            case DownLoadManger.STATE_DOWNLOADFAILED:
                mProgressView.setTv_note("请重试");
                mProgressView.setIv_icon(R.drawable.ic_redownload);
                break;
            case DownLoadManger.STATE_DOWNLOADED:
                mProgressView.setTv_note("安装");
                mProgressView.setIv_icon(R.drawable.ic_install);
                break;
            case DownLoadManger.STATE_INSTALLED:
                mProgressView.setTv_note("打开");
                mProgressView.setIv_icon(R.drawable.ic_install);
                break;
        }
    }

    /**
     * @return
     * @des 初始化持有视图，并找到需要的孩子对象
     * @call BaseHolder一旦初始化被调用
     */
    @Override
    public View initHolderView() {
        //初始化根布局
        View view = View.inflate(UIUtils.getContext(), R.layout.item_home_info, null);

        //找到孩子对象
        //我们用Butterknife来实现注入.这里还需要注解一下
        ButterKnife.bind(this, view);

        return view;
    }

    @OnClick(R.id.item_appinfo_progressView)
    public void onClick() {
          /*-------------3根据不同状态，响应不同操作----------------------*/
        DownLoadInfo downLoadInfo = DownLoadManger.getInstance().getDownLoadInfo(mInfoBean);
        LogUtils.d("downLoadTask", "downLoadInfo" + downLoadInfo.toString());
        int currentState = downLoadInfo.currentState;
        switch (currentState) {
            case DownLoadManger.STATE_UNDOWNLOAD:
                doDownLoad(downLoadInfo);
                break;
            case DownLoadManger.STATE_DOWNLOADING:
                pauseDownLoad(downLoadInfo);
                break;
            case DownLoadManger.STATE_PAUSEDOWNLOAD:
                doDownLoad(downLoadInfo);
                break;
            case DownLoadManger.STATE_WAITINGDOWNLOAD:
                cancelDownLoad(downLoadInfo);
                break;
            case DownLoadManger.STATE_DOWNLOADFAILED:
                doDownLoad(downLoadInfo);
                break;
            case DownLoadManger.STATE_DOWNLOADED:
                installApk(downLoadInfo);
                break;
            case DownLoadManger.STATE_INSTALLED:
                openApk(downLoadInfo);
                break;
        }
    }

    private void openApk(DownLoadInfo downLoadInfo) {
        CommonUtils.openApp(UIUtils.getContext(), downLoadInfo.packageName);
    }

    private void installApk(DownLoadInfo downLoadInfo) {
        File saveApk = new File(downLoadInfo.savePath);
        if (saveApk == null) {
            Toast.makeText(UIUtils.getContext(), "文件不存在", Toast.LENGTH_SHORT).show();
            return;
        }
        CommonUtils.installApp(UIUtils.getContext(), saveApk);
    }

    private void cancelDownLoad(DownLoadInfo downLoadInfo) {
        DownLoadManger.getInstance().cancelDownLoad(downLoadInfo);
    }

    private void pauseDownLoad(DownLoadInfo downLoadInfo) {
        //暂停我们交给DownLoadManager去处理,改变downLoadInfo状态
        DownLoadManger.getInstance().pauseDownLoad(downLoadInfo);
    }

    private void doDownLoad(DownLoadInfo downLoadInfo) {
        DownLoadManger.getInstance().doDownLoad(downLoadInfo);
    }

    @Override
    public void onDownLoadInfoChanged(final DownLoadInfo downLoadInfo) {
        //被观察者发布消息，每个人都值需影响自己有关的消息即可。要过滤传递过来的消息
        if (!downLoadInfo.packageName.equals(mInfoBean.packageName)) {
            return;
        }
        //刷新UI
        UIUtils.postTaskSafely(new Runnable() {
            @Override
            public void run() {
                refreshProgressView(downLoadInfo);
            }
        });
    }
}

